################################################################################
################################################################################
################################################################################
############### Analysis Script 1 ##############################################
############### Null Models, Anti-Establishment Rhetoric########################
############### Electoral Cycle Models##########################################
################################################################################
load("Data/EP_debates_07062023.Rdata")
library(lme4)
library(texreg)
library(sjPlot)
library(sjstats)
library(fixest)
library(ggplot2)

#####

levels(EP_debates$EU_party)
levels(EP_debates$EU_party) <- c("NGL", "EFD/EFDD", "ECR",
                                 "EDD", "ALDE",  "EPP",
                                 "Greens","S&D", "ID",
                                 "No Group", "UEN", "EFD/EFDD", 
                                 "NGL", "Greens", "No Group")
levels(EP_debates$EU_party)

### BEGIN BY CENTERING AND SCALING EVERYTHING

EP_debates$radical <- abs(EP_debates$lrgen - 5)
range(EP_debates$radical, na.rm = TRUE)

centFUN <- function(x) {
  x - mean(x, na.rm = TRUE)
}

result <- apply(EP_debates[,c(22:24, 27:71, 73:75, 77:107,117:118)], MARGIN = 2, FUN = centFUN)
result <- as.data.frame(result)


EP_eurobar_cent <- cbind(EP_debates[,-c(22:24, 27:71, 73:75, 77:107,117:118)], result)

rm(list=setdiff(ls(), "EP_eurobar_cent"))

EP_eurobar_cent$embed_dict_scale <- scale(EP_eurobar_cent$embed_dict)
EP_eurobar_cent$aut_dict_scale <- scale(EP_eurobar_cent$aut_dict)
EP_eurobar_cent$immi_dict_scale <- scale(EP_eurobar_cent$immi_dict)
EP_eurobar_cent$int_dict_scale <- scale(EP_eurobar_cent$int_dict)
EP_eurobar_cent$lrgen_scale <- scale(EP_eurobar_cent$lrgen)
EP_eurobar_cent$radical_scale <- scale(EP_eurobar_cent$radical)

EP_eurobar_cent$aut_sal <- scale(EP_eurobar_cent$mip_n_govdebt_ipol)
EP_eurobar_cent$immi_sal <- scale(EP_eurobar_cent$mip_n_immigration_ipol)
EP_eurobar_cent$eum_ipol <- scale(EP_eurobar_cent$eum_ipol)


### VARIANCE COMPONENTS MODELS FOR ALL OUTCOMES ###
Null_Model_AE <- lmer(embed_dict_scale ~ (1|mep_ids) + (1|party_id) + (1|country) + (1|year) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Null_Model_AE)

Null_Model_Immi <- lmer(immi_dict_scale ~ (1|mep_ids) + (1|party_id) + (1|country) + (1|year) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Null_Model_Immi)

Null_Model_Aut <- lmer(aut_dict_scale ~ (1|mep_ids) + (1|party_id) + (1|country) + (1|year) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Null_Model_Aut)

Null_Model_Int <- lmer(int_dict_scale ~ (1|mep_ids) + (1|party_id) + (1|country) + (1|year) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(Null_Model_Int)

texreg(list(Null_Model_AE, Null_Model_Aut,Null_Model_Immi, Null_Model_Int),
       file = "Tables/Variance_Models_120323.tex",
       include.ci = FALSE,
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.model.names = c("Anti-Establishment Rhetoric", "Austerity", "Immigration", "EU-Integration"),
       custom.note = "%stars.",
       digits = 2)

######## Anti-ESTABLISHMENT RHETORIC MAIN MODELS PAPER ##########################################

### no opposition control
AE_fit1 <- lmer(embed_dict_scale ~ challenge   + lrgen_scale  + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AE_fit1)

###  Opposition Control
AE_fit2 <- lmer(embed_dict_scale ~ challenge   + lrgen_scale  + EP_Coa +  nat_opp + de_macro + de_civil + de_health + 
                  de_agri + de_labour + de_edu + de_envi + de_energy + 
                  de_immi + de_transport + de_law + de_welfare  + de_commerce +
                  de_defense + de_techno + de_trade + de_intern + de_govern +
                  de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                  (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AE_fit2)



AE_fit_group <- lmer(embed_dict_scale ~ challenge   + lrgen_scale   + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(EU_party) +as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AE_fit_group)


texreg(list(AE_fit1, AE_fit2, AE_fit_group),
       file = "Tables/AE_models_Appendix_01032024.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition",
                              "challenge:right" = "Challenge * Right",
                              "challenge:left" = "Challenge * Left"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c("No", "No", "Yes")),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       label = "Table1",
       caption = "Structural Differences between Challenger and Dominant Parties")



EP_eurobar_cent$chall_fact <- as.factor(ifelse(EP_eurobar_cent$challenge == 1, "Challenger", "Mainstream"))

AE_fit_int_plot <- lmer(embed_dict_scale ~ chall_fact*right  + chall_fact*left   + de_macro + de_civil + de_health + 
                           de_agri + de_labour + de_edu + de_envi + de_energy + 
                           de_immi + de_transport + de_law + de_welfare  + de_commerce +
                           de_defense + de_techno + de_trade + de_intern + de_govern +
                           de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + as.factor(country) +
                           (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AE_fit_int_plot)

int_plot_right <- plot_model(AE_fit_int_plot, type = "emm",
                            terms = c("right", "chall_fact"),
                            title = "Marginal Effects of Party Ideology",
                            axis.title = c("Ideology (Center to Right) - CHES", "Anti-Establishment Rhetoric Score \n (standardized and centered around 0)"),
                            colors = "bw",
                            legend.title = "Party",
                            ci.lvl = .95, rg.limit = 66528) +
  theme(text = element_text(size = 16))+
  theme_classic()+
  scale_x_continuous(limits = c(0 ,5),
                     breaks = c(0, 1, 2, 3, 4, 5))
int_plot_right

int_plot_left <- plot_model(AE_fit_int_plot, type = "emm",
                             terms = c("left", "chall_fact"),
                             title = "Marginal Effects of Party Ideology",
                             axis.title = c("Ideology (Center to Left) - CHES", "Anti-Establishment Rhetoric Score \n (standardized and centered around 0)"),
                             colors = "bw",
                             legend.title = "Party",
                             ci.lvl = .95, rg.limit = 77616) +
  theme(text = element_text(size = 16))+
  theme_classic()+
  scale_x_continuous(limits = c(-5, 0),
                     breaks = c(-5, -4, -3, -2, -1, 0))
int_plot_left


int_AE_plots <- gridExtra::grid.arrange(int_plot_left, int_plot_right, ncol = 2)
ggsave("Figures/AE_plots_ideology_120124.png",int_AE_plots, width = 240, device = "png", height = 100, units = "mm", dpi=300, scale = 1.2)



poly_lr_model <- lmer(embed_dict_scale ~ challenge*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(poly_lr_model)


texreg(list(poly_lr_model),
       file = "Tables/AE_poly_model_01032024.tex",
       include.ci = FALSE,
       omit.coef = "(de)|(country)|(EU)|(year)|(proced)",
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes"),
                              "Year Fixed Effects" = c("Yes"),
                              "Country Fixed Effects" = c("Yes"),
                              "EP-Group Fixed Effects" = c("Yes")),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       label = "Table1",
       caption = "Interaction of Party Ideology and Party Type (incl. polynomial)")



poly_lr_model_plot <- lmer(embed_dict_scale ~ chall_fact*I(lrgen^2) + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ EU_party + as.factor(year) + country +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(poly_lr_model_plot)



poly_lr_plot <- plot_model(poly_lr_model_plot, type = "pred",
                             terms = c("lrgen[-5,-4,-3,-2,-1,0, 1, 2, 3 , 4, 5]", "chall_fact"),
                             title = "Marginal Effects of Party Ideology",
                             axis.title = c("Ideology (Left to Right) - CHES", "Anti-Establishment Rhetoric Score \n (standardized and centered around 0)"),
                             colors = "bw",
                             legend.title = "Party",
                             ci.lvl = .95, rg.limit = 74844) +
  theme(text = element_text(size = 16))+
  theme_classic()


poly_lr_plot

ggsave("Figures/poly_lr_plot_01032024.png",poly_lr_plot, width = 240, device = "png", height = 100, units = "mm", dpi=300, scale = 1.2)


######## Anti-ESTABLISHMENT RHETORIC FEs MODELS Appendix ##########################################

AE_fixed_YFCF <- feols(embed_dict_scale ~ challenge + lrgen_scale + EP_Coa +nat_opp | year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(AE_fixed_YFCF, cluster = "party_id")

AE_fixed_YFCF_CON <- feols(embed_dict_scale ~ challenge + lrgen_scale + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(AE_fixed_YFCF_CON, cluster = "party_id")

AE_fixed_YFCF_Group <- feols(embed_dict_scale ~ challenge + lrgen_scale + EP_Coa + nat_opp +  de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| EU_party + year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(AE_fixed_YFCF_Group, cluster = "party_id")

AE_fixed_YFCF_int_group <- feols(embed_dict_scale ~ challenge*right + challenge*left + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                             de_agri + de_labour + de_edu + de_envi + de_energy + 
                             de_immi + de_transport + de_law + de_welfare  + de_commerce +
                             de_defense + de_techno + de_trade + de_intern + de_govern +
                             de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5| EU_party + year + country , data = EP_eurobar_cent, cluster = "party_id")
summary(AE_fixed_YFCF_int_group, cluster = "party_id")



texreg(list(AE_fixed_YFCF, AE_fixed_YFCF_CON, AE_fixed_YFCF_Group, AE_fixed_YFCF_int_group),
       tex=TRUE, file ='Tables/AE_fixed_App_080623.tex',
       include.ci = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1),
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "challenge:right" = "Challenge * Right",
                              "challenge:left" = "Challenge * Left",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("No", "Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c("No", "No", "Yes", "Yes")),
       custom.note = "%stars. Clustered Standard Errors (National Party) in Parentheses",
       digits = 2,
       caption = "Structural Differences between Challenger and Dominant Parties")


######## Anti-ESTABLISHMENT RHETORIC MLM Alt Specs Appendix ##########################################


AE_randomslope_CON <- lmer(embed_dict_scale ~ challenge   + lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(country) +
                        (1|mep_ids) + (challenge|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AE_randomslope_CON)



AE_random_CON_DR <- lmer(embed_dict_scale ~ challenge   + lrgen_scale  + EP_Coa + nat_opp + as.factor(year) + as.factor(country) +
                             (1|mep_ids) + (1|party_id) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(AE_random_CON_DR)



texreg(list(AE_randomslope_CON, AE_random_CON_DR),
       file = "Tables/AE_alt_model_specs_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "No"),
                              "Year Fixed Effects" = c("Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes"),
                              "Random Slopes" = c("Yes", "No"),
                              "Random Intercepts (Debates)" = c("No", "Yes")),
       digits = 2,
      custom.note = "%stars. Random Intercepts for Parties and MEPs",
       caption = "Structural Differences between Challenger and Dominant Parties")

############### Anti_Establishment PARTY FIXED EFFECTS ########################


AE_random_CON_PF <- lmer(embed_dict_scale ~ dominant  +  lrgen_scale  + EP_Coa + nat_opp + de_macro + de_civil + de_health + 
                           de_agri + de_labour + de_edu + de_envi + de_energy + 
                           de_immi + de_transport + de_law + de_welfare  + de_commerce +
                           de_defense + de_techno + de_trade + de_intern + de_govern +
                           de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ as.factor(year) + as.factor(party_id) +
                           (1|mep_ids), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
texreg(AE_random_CON_PF)

texreg(list(AE_random_CON_PF),
       file = "Tables/AE_PartyFixed_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("dominant" = "Dominant Party",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes"),
                              "Year Fixed Effects" = c("Yes"),
                              "Party Fixed Effects" = c("Yes")),
       custom.model.names = c("Anti-Establishment Rhetoric"),
       custom.note = "%stars. Random Intercepts for MEPs",
       digits = 2,
       caption = "Within-Party Differences between Challenger and Dominant Status")




############### ANTI-ESTABLISHMENT CYCLE MODEL #################################

### For Plotting Only
EP_eurobar_cent$chall_fact <- as.factor(ifelse(EP_eurobar_cent$challenge == 1, "Challenger", "Mainstream"))
str(EP_eurobar_cent$chall_fact)
range(EP_eurobar_cent$days_from_EP)

EP_eurobar_cent$days_to_EP <- scale(EP_eurobar_cent$days_from_EP)# Make them more intuitive as of reviewer request
EP_eurobar_cent$days_to_nat <- scale(EP_eurobar_cent$days_from_nat)

cycle_AE <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + lrgen_scale  + de_macro + de_civil + de_health + 
                   de_agri + de_labour + de_edu + de_envi + de_energy + 
                   de_immi + de_transport + de_law + de_welfare  + de_commerce +
                   de_defense + de_techno + de_trade + de_intern + de_govern +
                   de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                   (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE)

cycle_AE_fit2 <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa  + lrgen_scale  + de_macro + de_civil + de_health + 
                   de_agri + de_labour + de_edu + de_envi + de_energy + 
                   de_immi + de_transport + de_law + de_welfare  + de_commerce +
                   de_defense + de_techno + de_trade + de_intern + de_govern +
                   de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                   (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_fit2)

cycle_AE_fit3 <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp*days_to_EP + nat_opp*days_to_nat + lrgen_scale  + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_fit3)


cycle_AE_fit_group <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat   + lrgen_scale  + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + EU_party + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_fit_group)


texreg(list(cycle_AE, cycle_AE_fit2, cycle_AE_fit3, cycle_AE_fit_group),
       file = "Tables/AE_cycle_240124.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "days_to_EP" = "Proximity to next EP-Election",
                              "days_to_nat" = "Proximity to next National Election",
                              "challenge:days_to_EP" = "Challenger * Proximity to next EP-Election",
                              "challenge:days_to_nat" = "Challenger * Proximity to next National Election",
                              "days_to_EP:nat_opp" = "Nat. Opp. * Proximity to next EP-Election ",
                              "days_to_nat:nat_opp"= "Nat. Opp. * Proximity to next National Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes", "Yes"),
                              "EP-Group Fixed Effects" = c("No", "No", "No", "Yes")),
       custom.model.names = c("Anti-Establishment Rhetoric", "Anti-Establishment Rhetoric", "Anti-Establishment Rhetoric", "Anti-Establishment Rhetoric"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Anti-Establishment Rhetoric conditional on Electoral Cycle")

############### ANTI-ESTABLISHMENT CYCLE MODEL APPENDIX #################################

cycle_AE_NC <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat + EP_Coa + nat_opp + lrgen_scale  + year + as.factor(country) +
                      (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))
cycle_AE_DR <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat + EP_Coa + nat_opp + lrgen_scale  + year + as.factor(country) +
                   (1|mep_ids) + (1|party_id) + (1|debate), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_NC)


texreg(list(cycle_AE_NC, cycle_AE_DR),
       file = "Tables/AE_cycl_altspecs_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "days_to_EP" = "Proximity to next EP-Election",
                              "days_to_nat" = "Proximity to next National Election",
                              "challenge:days_to_EP" = "Challenger * Proximity to next EP-Election",
                              "challenge:days_to_nat" = "Challenger * Proximity to next National Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("No", "No"),
                              "Year Fixed Effects" = c("Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes"),
                              "Random Intercepts (Debates)" = c("No", "Yes")),
       custom.model.names = c("Anti-Establishment Rhetoric", "Anti-Establishment Rhetoric"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Anti-Establishment Rhetoric conditional on Electoral Cycle")

############### ANTI-ESTABLISHMENT FIXED MODEL APPENDIX #################################


cycle_fixed <- feols(embed_dict_scale ~ challenge*days_to_EP + challenge*days_to_nat + lrgen_scale + EP_Coa  + nat_opp + de_macro + de_civil + de_health + 
                       de_agri + de_labour + de_edu + de_envi + de_energy + 
                       de_immi + de_transport + de_law + de_welfare  + de_commerce +
                       de_defense + de_techno + de_trade + de_intern + de_govern +
                       de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 | year + country, data = EP_eurobar_cent, cluster = "party_id", subset = !is.na(EP_eurobar_cent$party_id))
summary(cycle_fixed, cluster = "party_id")


texreg(list(cycle_fixed),
       file = "Tables/AE_cycle_fix_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "days_to_EP" = "Proximity to next EP-Election",
                              "days_to_nat" = "Proximity to next National Election",
                              "challenge:days_to_EP" = "Challenger * Proximity to next EP-Election",
                              "challenge:days_to_nat" = "Challenger * Proximity to next National Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes"),
                              "Year Fixed Effects" = c("Yes"),
                              "Country Fixed Effects" = c("Yes")),
       custom.model.names = c("Anti-Establishment Rhetoric"),
       digits = 2,
       custom.note = "%stars. Clustered Standard Errors (National Party) in Parentheses",
       caption = "Anti-Establishment Rhetoric conditional on Electoral Cycle")




###PLOTS
#### USE THE UNSCALED VERSION FOR THE PLOTTING TO BE ABLE TO MEANINGFULLY LABEL X AXIS
cycle_AE <- lmer(embed_dict_scale ~ chall_fact*days_from_EP  + chall_fact*days_from_nat + EP_Coa + nat_opp + lrgen_scale  + de_macro + de_civil + de_health + 
                   de_agri + de_labour + de_edu + de_envi + de_energy + 
                   de_immi + de_transport + de_law + de_welfare  + de_commerce +
                   de_defense + de_techno + de_trade + de_intern + de_govern +
                   de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                   (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

cycle_EP_plot <- plot_model(cycle_AE, type = "emm",
                            terms = c("days_from_EP", "chall_fact"),
                            title = "Marginal Effects of Proximity to next European Election",
                            axis.title = c("Time till next European Election", "Anti-Establishment Rhetoric Score \n (standardized and centered around 0)"),
                            colors = "bw",
                            legend.title = "Party",
                            ci.lvl = .95) +
                            theme(text = element_text(size = 16))+
                            theme_classic()+
                            scale_x_continuous(limits = c(-1000, 0),
                            breaks = c(-720, -360, -180, 0),
                            labels = c("Two Years", "One year", "Six months", "Election day"),
                            guide = guide_axis(angle = -45))
cycle_EP_plot


cycle_nat_plot <- plot_model(cycle_AE, type = "emm",
                             terms = c("days_from_nat", "chall_fact"),
                             title = "Marginal Effects of Proximity to next National Election",
                             axis.title = c("Time till next National Election", "Anti-Establishment Rhetoric Score \n (standardized and centered around 0)"),
                             colors = "bw",
                             legend.title = "Party",
                             ci.lvl = .95) +
                             theme(text = element_text(size = 16))+
                             theme_classic()+
                             scale_x_continuous(limits = c(-1200, 0),
                                               breaks = c(-1080, -720, -360, -180, 0),
                                               labels = c("Three years", "Two Years", "One year", "Six months", "Election day"),
                                               guide = guide_axis(angle = -45))

cycle_nat_plot



cycle_AE_plots <- gridExtra::grid.arrange(cycle_EP_plot, cycle_nat_plot, ncol = 2)
ggsave("Figures/cycle_AE_plots_12022024.png",cycle_AE_plots, width = 240, device = "png", height = 100, units = "mm", dpi=300, scale = 1.2)


############ RUN ANALYSIS SEPARATELY FOR LEFT AND RIGHT CHALLENGERS
EP_eurobar_left <- subset(EP_eurobar_cent, EP_eurobar_cent$lrgen < 0)

cycle_AE_left <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp*days_to_EP + nat_opp*days_to_nat + lrgen_scale  + de_macro + de_civil + de_health + 
                   de_agri + de_labour + de_edu + de_envi + de_energy + 
                   de_immi + de_transport + de_law + de_welfare  + de_commerce +
                   de_defense + de_techno + de_trade + de_intern + de_govern +
                   de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                   (1|mep_ids) + (1|party_id), data = EP_eurobar_left, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_left)

EP_eurobar_right <- subset(EP_eurobar_cent, EP_eurobar_cent$lrgen > 0)

cycle_AE_right <- lmer(embed_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp*days_to_EP + nat_opp*days_to_nat + lrgen_scale  + de_macro + de_civil + de_health + 
                        de_agri + de_labour + de_edu + de_envi + de_energy + 
                        de_immi + de_transport + de_law + de_welfare  + de_commerce +
                        de_defense + de_techno + de_trade + de_intern + de_govern +
                        de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+ year + as.factor(country) +
                        (1|mep_ids) + (1|party_id), data = EP_eurobar_right, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_AE_right)


### RESULTS APPLY TO BOTH TYPES OF CHALLENGERS 

texreg(list(cycle_AE_left, cycle_AE_right),
       file = "Tables/AE_cycl_LR_080224.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "days_to_EP" = "Proximity to next EP-Election",
                              "days_to_nat" = "Proximity to next National Election",
                              "challenge:days_to_EP" = "Challenger * Proximity to next EP-Election",
                              "challenge:days_to_nat" = "Challenger * Proximity to next National Election",
                              "days_to_EP:nat_opp" = "Nat. Opp. * Proximity to next EP-Election ",
                              "days_to_nat:nat_opp"= "Nat. Opp. * Proximity to next National Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes"),
                              "EP-Group Fixed Effects" = c("No", "No")),
       custom.model.names = c("Left Parties", "Right Parties"),
       custom.note = "%stars. Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Anti-Establishment Rhetoric conditional on Electoral Cycle for Left and Right Parties")








###### HOW DOES ISSUE_EMPHASIS CHANGE IN RESPONSE TO THE ELECTORAL CYCLE MODELS

cycle_Immi <- lmer(immi_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp + lrgen_scale + de_macro + de_civil + de_health + 
                   de_agri + de_labour + de_edu + de_envi + de_energy + 
                   de_immi + de_transport + de_law + de_welfare  + de_commerce +
                   de_defense + de_techno + de_trade + de_intern + de_govern +
                   de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(country) +
                   (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_Immi)

#### Austerity

cycle_Aut <- lmer(aut_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp + lrgen_scale + de_macro + de_civil + de_health + 
                     de_agri + de_labour + de_edu + de_envi + de_energy + 
                     de_immi + de_transport + de_law + de_welfare  + de_commerce +
                     de_defense + de_techno + de_trade + de_intern + de_govern +
                     de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5 + as.factor(country) +
                      (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_Aut)


#### EU-Integration

cycle_Int <- lmer(int_dict_scale ~ challenge*days_to_EP  + challenge*days_to_nat  + EP_Coa + nat_opp + lrgen_scale + de_macro + de_civil + de_health + 
                    de_agri + de_labour + de_edu + de_envi + de_energy + 
                    de_immi + de_transport + de_law + de_welfare  + de_commerce +
                    de_defense + de_techno + de_trade + de_intern + de_govern +
                    de_lands + de_culture + proced_1 + proced_2 + proced_3 +proced_4 + proced_5+  as.factor(country) +
                     (1|mep_ids) + (1|party_id), data = EP_eurobar_cent, control = lmerControl(optimizer = "Nelder_Mead"))

texreg(cycle_Int)


texreg(list(cycle_Aut, cycle_Immi, cycle_Int),
       file = "Tables/issues_cycle_080623.tex",
       include.ci = FALSE,
       custom.coef.map = list("challenge" = "Challenger",
                              "days_to_EP" = "Proximity to next EP-Election",
                              "days_to_nat" = "Proximity to next National Election",
                              "challenge:days_to_EP" = "Challenger * Proximity to next EP-Election",
                              "challenge:days_to_nat" = "Challenger * Proximity to next National Election",
                              "lrgen_scale" = "LR-Position",
                              "EP_Coa" = "EP-Opposition",
                              "nat_opp" = "Nat. Opposition"),
       booktabs = TRUE,
       dcolumn = FALSE,
       custom.gof.rows = list("Topic Controls" = c("Yes", "Yes", "Yes"),
                              "Year Fixed Effects" = c("Yes", "Yes", "Yes"),
                              "Country Fixed Effects" = c("Yes", "Yes", "Yes")),
       custom.model.names = c("Austerity", "Immigration", "EU-Integration"),
       custom.note = "Random Intercepts for Parties and MEPs",
       digits = 2,
       caption = "Issue-Emphasis conditional on Electoral Cycle")


#### CHECKED AND RUN ON 08.06.2023
#### CHANGES MADE on 12 FEB 2024 to accommodate JEPP REVIEWER COMMENTS




